package cn.zjh.basic.controller;

import cn.zjh.basic.dto.SmsCodeDto;
import cn.zjh.basic.exception.BusinessException;
import cn.zjh.basic.service.IVerifyCodeService;
import cn.zjh.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/verifyCode")
@RestController
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService iVerifyCodeService;
    @GetMapping("/image/{key}")
    public AjaxResult imag(@PathVariable("key") String key){
        try {
            String image = iVerifyCodeService.image(key);
            return AjaxResult.me().setResultObj(image);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败！！");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            iVerifyCodeService.smsCode(smsCodeDto);
            return new AjaxResult();
            //只要有业务异常都要加
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取手机验证码失败");
        }
    }


}
